﻿using System;
using System.Text;
using System.Net;
using System.Threading;
using System.IO;

namespace Minecraft_Xenon
{
    public class Downloader
    {
        private string URL;
        private string FileName;

        private float Percent = 0;
        private int Speed = 0;

        private WebClient WebClient;
        private WebRequest WebRequest;
        private WebResponse WebResponse;
        private byte[] Buffer = new byte[2048];

        private int Length = 0;
        private int DownloadedLength = 0;
        private int LastLength = 0;
        private System.Timers.Timer Timer;

        private bool Complete;

        /// <summary>
        /// 下载JRE
        /// </summary>
        /// <param name="url"></param>JRE下载地址
        /// <param name="path"></param>保存路径
        public Downloader(string url, string filename)
        {
            URL = url;
            FileName = filename;
        }

        /// <summary>
        /// 取得下载进度
        /// </summary>
        /// <returns></returns>
        public float GetProgress()
        {
            return Percent;
        }

        /// <summary>
        /// 取得下载速度
        /// </summary>
        /// <returns></returns>
        public int GetSpeed()
        {
            return Speed;
        }

        public bool IsCompleted()
        {
            return Complete;
        }

        private void calculateSpeed(object source, System.Timers.ElapsedEventArgs e)
        {
            Speed = (DownloadedLength - LastLength) / 1024;
            LastLength = DownloadedLength;
            Percent = (float)((float)DownloadedLength / (float)Length);

            if (Percent == 1)
            {
                Complete = true;
            }
        }

        /// <summary>
        /// 开始后台下载
        /// </summary>
        public void Start()
        {
            Thread t = new Thread(new ThreadStart(Download));
            t.Name = "Downloading Thread";
            t.Start();
        }

        /// <summary>
        /// 手动下载
        /// </summary>
        public void Download()
        {
            if (!File.Exists(FileName))
            {
                File.Delete(FileName);
            }

            WebRequest = WebRequest.Create(URL);
            WebResponse = WebRequest.GetResponse();

            Length = (int) WebResponse.ContentLength;

            FileStream fs = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            Stream stream = WebResponse.GetResponseStream();

            Timer = new System.Timers.Timer(500);
            Timer.Elapsed += new System.Timers.ElapsedEventHandler(calculateSpeed);
            Timer.Enabled = true;

            int i;
            while ((i = stream.Read(Buffer, 0, Buffer.Length)) > 0)
            {
                DownloadedLength += i;
                fs.Write(Buffer, 0, i);
            }
            fs.Close();

            Complete = true;
        }

        
    }
}
